﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EMANIFEST.Common;
using EMANIFEST.Common.Validation;
using EMANIFEST.Dtos;
using EMANIFEST.ImportExcelData;
using EMANIFEST.eManifestService;
using EMANIFEST.ImageService;

namespace EMANIFEST.CONTROLTEMPLATES.Emanifest_UI
{
    public partial class UploadImageFile : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //Danh sach cang
                List<PortDto> lstPort = Cache[CacheKey.CacheNameOfListPortInVN] as List<PortDto>;

                if (lstPort == null)
                {
                    EmanifestServiceClient client = new EmanifestServiceClient();
                    lstPort = client.GetListPortInVN();
                    client.Close();
                }
                
                ddlPort.DataSource = lstPort;
                ddlPort.DataTextField = "Name";
                ddlPort.DataValueField = "PortId";
                ddlPort.DataBind();

                if (Cache[CacheKey.CacheNameOfListPortInVN] == null)
                    Cache.Insert(CacheKey.CacheNameOfListPortInVN, lstPort, null, DateTime.Now.AddHours(24), TimeSpan.Zero);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string rid = Request.QueryString["rid"];
            if (Session["DocumenType"] == null)
                return;
            if (string.IsNullOrEmpty(rdnStatus.SelectedValue))
            {
                lblMessage.Text = "Bạn phải chọn loại chứng từ";
                string msg = lblMessage.Text;
                EmanifestCommon.ShowMessage(this.Page, msg);
                return;
            }

            if (fileUpload.HasFile)
            {
                string ext = fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf('.') + 1);
                //Validate
                if (!ValidationHelper.AllowExtensionImageUpload(ext.ToLower().Trim()))
                {
                    lblMessage.Text = "File không hợp lệ. Hệ thống chỉ hỗ trợ upload gif,png,jpg!";
                    string msg = lblMessage.Text;
                    EmanifestCommon.ShowMessage(this.Page, msg);
                    return;
                }
                BillImageDto bill = new BillImageDto();
                bill.IsMaster = rdnStatus.SelectedValue == "0" ? false : true;
                bill.CreatedDate = DateTime.Now;
                bill.DocumentId = new Guid(rid);
                bill.CreaterId = EmanifestUser.FBAUserId;
                bill.ImageName = fileUpload.FileName;
                bill.ImageStream = fileUpload.FileBytes;
                bill.ReceivePortId = rdnStatus.SelectedValue == "0"  ? new Guid(ddlPort.SelectedValue) : Guid.Empty;

                ImageServiceClient image = new ImageServiceClient();
                ResponseDto reponse = image.UploadImage(bill);

                if (reponse.IsSuccess)
                {
                    EmanifestCommon.ShowMessage(this.Page, "Upload thành công.");
                }
                else
                {
                    EmanifestCommon.ShowMessage(this.Page, "Upload không thành công.");
                }
            }
        }
    }
}
